package br.com.gaacorp.sai.webapp.action;

import java.io.Serializable;

import br.com.gaacorp.sai.model.Matter;
import br.com.gaacorp.sai.service.GenericManager;

public class MatterForm extends BasePage implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5417956858916947251L;
	private GenericManager<Matter, Long> matterManager;
	private Matter matter = new Matter();
	private Long matterId;

	public MatterForm() {
	}

	public String delete() {
		matterManager.remove(matter.getMatterId());
		addMessage("matter.deleted");

		return "matterList";
	}

	public String edit() {
		// Comparison to zero (vs. null) is required with MyFaces 1.2.2, not
		// with previous versions
		if (matterId != null && matterId != 0) {
			matter = matterManager.get(matterId);
		} else {
			matter = new Matter();
		}

		return "matterForm";
	}

	public Matter getMatter() {
		return matter;
	}

	public Long getMatterId() {
		return matterId;
	}

	public GenericManager<Matter, Long> getMatterManager() {
		return matterManager;
	}

	public String save() {
		boolean isNew = (matter.getMatterId() == null || matter.getMatterId() == 0);
		matterManager.save(matter);

		String key = (isNew) ? "matter.added" : "matter.updated";
		addMessage(key);

		if (isNew) {
			return "matterList";
		} else {
			return "matterForm";
		}
	}

	public void setMatter(Matter matter) {
		this.matter = matter;
	}

	public void setMatterId(Long matterId) {
		this.matterId = matterId;
	}

	public void setMatterManager(GenericManager<Matter, Long> matterManager) {
		this.matterManager = matterManager;
	}
}